package me.zhengjie.modules.web.rest.course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.PageData;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.course.*;
import me.zhengjie.modules.web.service.course.ICourseService;
import me.zhengjie.request.ATMRContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "前端接口-课程")
@RequestMapping("web/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    @PostMapping("index")
    @ApiOperation("前端接口-课程-首页")
    public ResponseBaseEntity index() {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<CourseIndexResp> list = courseService.courseIndex();
            response.setBody(list);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("list")
    @ApiOperation("前端接口-课程-列表")
    public ResponseBaseEntity list(@RequestBody CourseListReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<CourseIndexResp> list = courseService.courseList(req);
            response.setBody(list);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("detail")
    @ApiOperation("前端接口-课程-详情")
    public ResponseBaseEntity detail(@RequestBody CourseDetailReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            CourseDetailResp detailResp = courseService.courseDetail(req.getCourseNo());
            response.setBody(detailResp);
        } catch (WeChatException e) {
            response.getHeader().setErrCode(e.getCodeStr());
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("clockIn")
    @ApiOperation("前端接口-课程-打卡")
    public ResponseBaseEntity clockIn(@RequestBody CourseClockInReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            boolean result = courseService.clickIn(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("clockInList")
    @ApiOperation("前端接口-课程-打卡列表")
    public ResponseBaseEntity clockInList(@RequestBody CourseClockInListRequest req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<CourseClockInListResp> result = courseService.clockInListV2(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
    @PostMapping("clockInPageList")
    @ApiOperation("前端接口-课程-打卡分页列表")
    public ResponseBaseEntity clockInPageList(@RequestBody CourseClockInListRequest req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            PageData<CourseClockInListResp> result = courseService.clockInListV4(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("clockInDetail")
    @ApiOperation("前端接口-课程-打卡详情")
    public ResponseBaseEntity clockInDetail(@RequestBody CourseClockInDetailRequest req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            req.setOpenId(ATMRContext.getContext().getOpenId());
            CourseClockInDetailResp result = courseService.clockInDetail(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("getSignInDetail")
    @ApiOperation("前端接口-课程-获取签到详情")
    public ResponseBaseEntity getSignInDetail(@RequestBody CourseSignInDetailReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            CourseSignInDetailResp result = courseService.getSignInDetail(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode(e.getCodeStr());
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("signin")
    @ApiOperation("前端接口-课程-签到")
    public ResponseBaseEntity signin(@RequestBody CourseSignInReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            CourseSignInResp result = courseService.signin(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("getMealsByMobile")
    @ApiOperation("前端接口-课程-签到-查询是否需要缴纳餐费")
    public ResponseBaseEntity getMealsByMobile(@RequestBody CourseSignInMealsReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            CourseSignInMealsResp result = courseService.getMealsByMobile(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("addApplicants")
    @ApiOperation("前端接口-课程-添加报名人")
    public ResponseBaseEntity addApplicants(@RequestBody CourseApplicantsReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        /*try {
            boolean result = courseService.addApplicants(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }*/
        return response;
    }

    @PostMapping("applicantsList")
    @ApiOperation("前端接口-课程-报名人列表")
    public ResponseBaseEntity applicantsList(@RequestBody CourseApplicantsReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<CourseApplicantsResp> result = courseService.applicantsList(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}
